import java.util.ArrayList;
import java.util.List;

public class Solution1024 {
    public int videoStitching(int[][] clips, int time) {
        List<int[]> list=new ArrayList<>();
        for (int[] clip : clips) {
            list.add(clip);
        }
        list.sort((a,b)->a[0]==b[0]?b[1]-a[1]:a[0]-b[0]);
        if (list.get(0)[0]!=0){
            return -1;
        }
        int ans=1,cur=list.get(0)[1],c=1;
        if (cur>=time){
            return 1;
        }
        while (c<clips.length){
            int dis=cur;
            while (c<list.size() && list.get(c)[0]<=cur){
                dis=Math.max(dis,list.get(c)[1]);
                c++;
            }
            if (dis==cur){
                return -1;
            }
            ans++;
            cur=dis;
            if (cur>=time){
                return ans;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1024().videoStitching(new int[][]{{0,5},{6,8}},7));
    }
}
